import time
import os
import django
from celery import Celery
from django.core.mail import send_mail
from django.conf import settings


app = Celery("celery_tasks.tasks", broker="redis://127.0.0.1:6379/4")

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'haima.settings')

django.setup()

# 定义任务函数
@app.task
def send_active_email(to_email, username, token):
    """发送用户激活邮件"""
    # 邮件标题
    subjiect = '海马升仙欢迎你'
    # 发件人
    sender = settings.EMAIL_FROM
    # 邮件正文
    message = '欢迎你'
    # 收件人
    receiver = [to_email]
    html_message = """
         <h1>%s 恭喜您成为海马生鲜注册会员</h1><br/>
         <h3>请您在1小时内点击以下链接进行账户激 活</h3>
         <a href="http://127.0.0.1:8000/active/%s">
         http://127.0.0.1:8000/user/active/%s</a> """ % (username, token, token)
    send_mail(subjiect, message, sender, receiver, html_message=html_message)
    # 为了体现出celery异步完成发送邮件，这里睡眠5秒
    time.sleep(5)












